package com.bw.module_mine.ui.login.repository


import com.bw.module_mine.http.MineApi
import com.bw.module_mine.model.LoginPostModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

/**
 * @author zhy
 * @date 2025-09-23 13:41
 * @description
 */
class LoginRepository @Inject constructor() {
    @Inject
    lateinit var api: MineApi

    /**
     * 发送登录验证码
     */
    fun sendLoginCode(phone: String) = flow {
        emit(api.sendLoginCode(phone = phone))
    }.flowOn(Dispatchers.IO)

    /**
     * 发送重置验证码
     */
    fun sendResetCode( phone: String) = flow {
        emit(api.sendResetCode(phone = phone))
    }.flowOn(Dispatchers.IO)

    /**
     * 登录：短信登录、账号密码登录
     */
    fun login(grantType:String = "sms",smsCode:String = "",phone: String = "", password: String = "",newPassword:String = "") = flow {
        val body = LoginPostModel(
            grantType = grantType,
            newPassword = newPassword,
            operatorType = 2,
            password = password,
            smsCode = smsCode,
            userName = phone
        ).toBody()
        emit(api.login(body))
    }.flowOn(Dispatchers.IO)

    /**
     * 重置密码
     *
     */
    fun resetPwd(grantType:String = "sms",smsCode:String = "",phone: String = "", password: String = "",newPassword:String = "") = flow {
        val body = LoginPostModel(
            grantType = grantType,
            newPassword = newPassword,
            operatorType = 2,
            password = password,
            smsCode = smsCode,
            userName = phone
        ).toBody()
        emit(api.resetPwd(body))
    }.flowOn(Dispatchers.IO)

    /**
     * 修改密码
     */
    fun newPwd(grantType:String = "sms",smsCode:String = "",phone: String = "", password: String = "",newPassword:String = "") = flow {
        val body = LoginPostModel(
            grantType = grantType,
            newPassword = newPassword,
            operatorType = 2,
            password = password,
            smsCode = "0",
            userName = phone
        ).toBody()
        emit(api.newPwd(body))
    }.flowOn(Dispatchers.IO)
}